package my2Cents;

import java.awt.Color;
import java.awt.Graphics2D;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.BodyList;
import net.phys2d.raw.StaticBody;
import net.phys2d.raw.shapes.PCircle;
import net.phys2d.raw.shapes.PhysShape;


public strictfp class Bomb extends Body implements SpecialBody {
	private float time = 0.0f;
	private int red = 0;
	private int lastTick = 0;
	private int power = 120;
	private boolean makeFragments = false;
	private BodyList affected;
	
	public Bomb(PhysShape shape, float m, int delay, BodyList affected, boolean isStatic) {
		super(shape, m);
		if(isStatic) {
			this.setMoveable(false);
		}
		this.time = delay;
		this.affected = affected;
		shape.setName("Bomb");
	}
	
	public void setPower(int pow) {
		power = pow;
	}
	
	public void makeFragments(boolean b) {
		makeFragments = b;
	}
	
	/*public void collided(Body other) {
		super.collided(other);
		if(!(other instanceof StaticBody)) {
			time = -1;
		}
	}*/
	
	public void think() {
		super.think();
		time-=0.02f;
		
		red -= 8;
		red = Math.max(red,0);
		if((int) time != lastTick) {
			red = 255;
			lastTick = (int) time;
			//this.doOutlineFx();
		}
		if(time < .2) {
			red = 255;
			//this.doOutlineFx();
		}
		
		this.setFillColor(new Color(255,255-red,255-red));
		if(time < 0) {
			explode();
			Sandbox.getUndoList().undo(this, null);
			remove();
		}
	}
	
	protected void drawCenter(Graphics2D g, Vector2f pos, float rot) {
		
	}
	
	public void draw(Graphics2D g) {
		Vector2f p1 = (Vector2f) this.getPosition();
		//g.drawLine((int)p1.x + 5, (int)p1.y - 5, (int)p1.x + 20, (int)p1.y - 20);

		super.draw(g);

		g.setColor(Color.BLACK);
		String timeStr = "" + (int)Math.ceil(time);
		int x = (int) (p1.x-3);
		if(timeStr.length() > 1) x -= 3;
		g.drawString(timeStr,x,p1.y+5);
	}

	private void debris() {
		int cnt = new Random().nextInt(6)+4;
		for(int i=0; i<cnt; i++) {
			PCircle shp = new PCircle(4);
			final Body ball = new Body(shp,4);
			Sandbox.getWorld().add(ball);
			Vector2f pos = (Vector2f) this.getPosition();
			float rx = (float) (new Random().nextFloat()-.5);
			float ry = (float) (new Random().nextFloat()-.5);
			ball.setPosition(pos.x + rx, (pos.y + ry));
			ball.setRestitution(0.6f);
			TimerTask task = new TimerTask() {
				public void run() {
					ball.remove();
				}
			};
			Timer t = new Timer();
			t.schedule(task, new Random().nextInt(1000) + 2000);
		}		
	}
	
	private void explode() {
		if(makeFragments) debris();
		Undo me = Sandbox.getUndoList().getUndo(this,"Bomb");
		if(me != null) {
			me.performUndo();
		}
		for(int i=0; i<affected.size(); i++) {
			Body b = affected.get(i);
			Vector2f delta = new Vector2f();
			
			Vector2f p1 = (Vector2f) this.getPosition();
			Vector2f p2 = (Vector2f) b.getPosition();
			
			float dist = p1.distance(p2);
			dist = Math.min(dist,120);
			
			delta.x = (p2.x - p1.x);
			delta.y = (p2.y - p1.y);
			delta.normalise();
			
			delta.x *= (120 - dist);
			delta.y *= (120 - dist);
			
			delta.normalise();
			delta.scale(power);
			delta.scale((new Random().nextFloat()/2)+0.5f);
			
			Random r = new Random();
			
			b.adjustAngularVelocity((float) ((r.nextFloat()-.5)*(120-dist)/15));
			b.adjustVelocity(delta);
		}
	}
	
	public Body duplicate() {
		Bomb nb = new Bomb(getShape(), getMass(), lastTick, Sandbox.getWorld().getBodies(), isStatic());
		nb.red = this.red;
		nb.affected = this.affected;
		nb.lastTick = this.lastTick;
		nb.makeFragments = this.makeFragments;
		nb.power = this.power;
		nb.time = this.time;
		nb.setPosition(this.getPosition().getX(), this.getPosition().getY());
		nb.setRotation(this.getRotation());
		return nb;
	}
}
